import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

class PersistInstance {
  static late SharedPreferences _localStorage;

  static SharedPreferences get localStorage => _localStorage;

  static init() async {
    _localStorage = await SharedPreferences.getInstance();
  }
}

mixin Persist {
  late String key;

  Map<String, dynamic> persistInit({
    required String key,
    required Map<String, dynamic> defaultData,
  }) {
    this.key = key;
    var _data = PersistInstance.localStorage.getString(key);
    return _data != null ? jsonDecode(_data) : defaultData;
  }

  persistSave(Map<String, dynamic> data) {
    PersistInstance.localStorage.setString(this.key, jsonEncode(data));
  }
}
